package com.lt.contract.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.lt.contract.bean.utils.Constant;
import com.lt.contract.bean.utils.ExcelUtil;
import com.lt.contract.entity.NmCirciuitRealation;
import com.lt.contract.entity.NmOneStopService;
import com.lt.contract.entity.ZqSgjs;
import com.lt.contract.service.ZqSgjsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * (ZqSgjs)表控制层
 *
 * @author makejava
 * @since 2020-03-31 21:16:26
 */
@RestController
@Slf4j
@RequestMapping("zqSgjs")
public class ZqSgjsController {
    /**
     * 服务对象
     */
    @Resource
    private ZqSgjsService zqSgjsService;

    @Resource
    NmRoleController controller;


    /**
     * 获取  一站式导入列表
     * @param params
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @GetMapping("/queryAll")
    public JSONArray queryAll(@RequestParam String params, HttpServletRequest request){
        System.out.println(params);
        List<ZqSgjs> contractList = null;
        try {
            params =  controller.getRole(params);
            Map<String,Object> map = JSON.parseObject(params);
            contractList = zqSgjsService.queryAll(map);
            JSONArray result = JSON.parseArray(JSON.toJSONString(contractList));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 获取  一站式导入列表总数
     * @param params
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @GetMapping("/queryAllCount")
    public Long queryAllCount(@RequestParam String params){
        try {
            params =  controller.getRole(params);
            Map<String,Object> map = JSON.parseObject(params);
            Long count = zqSgjsService.queryCount(map);
            return count;
        }catch (Exception e){
            e.printStackTrace();
            return 0L;
        }

    }

    @GetMapping(value = "/download")
    @CrossOrigin
    public void exportList(@RequestParam String params, HttpServletResponse response) {
        log.info("zqSgjs download");
        try {
            params =  controller.getRole(params);
            Map<String,Object> map = JSON.parseObject(params);
            List<ZqSgjs> nmCirciuitRealations = zqSgjsService.exportList(map);
            String fileName = "手工计收明细"+System.currentTimeMillis() + ".xlsx";
            ExcelUtil.writeExcel(response,nmCirciuitRealations,fileName,"手工计收明细",new ZqSgjs());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
